package nc.bs.gl.eventlistener.bmcontrol;

import java.util.ArrayList;
import java.util.List;

import nc.bs.framework.common.InvocationInfoProxy;
import nc.bs.framework.common.NCLocator;
import nc.bs.gl.hrpbctprule.constants.GlControlRuleConst;
import nc.bs.gl.hrpbctprule.util.GlControlRuleUtil;
import nc.bs.gl.pubinterface.IVoucherAbandon;
import nc.bs.gl.voucher.VoucherBO;
import nc.itf.hrpbm.budgetctrl.service.IBmBudgetControlService;
import nc.pubitf.initgroup.InitGroupQuery;
import nc.vo.arap.bx.util.BXUtil;
import nc.vo.gl.pubinterface.VoucherOperateInterfaceVO;
import nc.vo.gl.pubvoucher.OperationResultVO;
import nc.vo.gl.pubvoucher.VoucherVO;
import nc.vo.pub.BusinessException;

public class BmVoucherAbandon implements IVoucherAbandon {

    List<OperationResultVO> resultList = new ArrayList<OperationResultVO>();

    @Override
    public OperationResultVO[] afterAbandon(VoucherOperateInterfaceVO vo)
            throws BusinessException {

        boolean isprojControlUsed = (BXUtil.isProductTbbInstalled("M0Z8")
                && InitGroupQuery.isEnabled(InvocationInfoProxy.getInstance().getGroupId(), "M0Z8"));
        if (!isprojControlUsed)
            return null;

//        VoucherBO bo = new VoucherBO();
//        VoucherVO[] vouchers = bo.queryByPks(vo.pk_vouchers);

        VoucherVO[] vouchers = null;

        //ADD BY WW 2025-02-17 提高重算的效率，重算时先从userdata中结束数据
        if (vo.userdata != null && vo.userdata.getClass().isArray()) {
            if (((Object[]) vo.userdata).length == 2 && "VoucherReBusiCtrl".equals(((Object[]) vo.userdata)[0])) {
                vouchers = new VoucherVO[]{(VoucherVO) ((Object[]) vo.userdata)[1]};
            }
        } else {
            VoucherBO bo = new VoucherBO();
            vouchers = bo.queryByPks(vo.pk_vouchers);

        }
        for(int i=0;i<vouchers.length;i++){

            String actionCode = null;

            if(vouchers[i].getDiscardflag()!=null && vouchers[i].getDiscardflag().booleanValue()){//作废

                actionCode = "INVALID";

            }else{//取消作废

                actionCode = "UNINVALID";

            }
            //判断业务单据配置节点,是否开启了总账控制规则,如果开启,则走新逻辑,否则走旧的业务逻辑
            if (GlControlRuleUtil.enableGl(GlControlRuleConst.BILLTYPE, null)) {
                //我的新逻辑
                GlControlRuleUtil.processBudgetControl(actionCode,vouchers[i],
                        null);
                continue;

            }

            //获取预算控制VO：收入、支出进行分离
            BmCtrlVoucherVOGenerator ctrlvoGenerator = new BmCtrlVoucherVOGenerator();
            AggBmCtrlVoucherVO[] ctrlAggVos = ctrlvoGenerator.generateBmCtrlVO(vouchers[i]);

            /*
             * 执行预算控制
             * 先执行收入、再执行支出（支出、资本）
             */
            String topBudgetCode = null;
            AggBmCtrlVoucherVO ctrlAggVo = null;

            /*
             * 执行支出预算
             */
            AggBmCtrlVoucherVO ctrlAggVo_OUT = ctrlAggVos == null ? null : ctrlAggVos[1];
            topBudgetCode = "YS02";
            //再次处理维度信息
            ctrlAggVo = ctrlvoGenerator.processDimValueAgain(topBudgetCode, ctrlAggVo_OUT);

            this.budgetCtrl(topBudgetCode, "支出", actionCode, vouchers[i], ctrlAggVo, null);


            /*
             * 执行资本预算
             */
            topBudgetCode = "YS03";
            //再次处理维度信息
            ctrlAggVo = ctrlvoGenerator.processDimValueAgain(topBudgetCode, ctrlAggVo_OUT);

            this.budgetCtrl(topBudgetCode, "支出", actionCode, vouchers[i], ctrlAggVo, null);

            /*
             * 执行资金预算-支出
             */
            topBudgetCode = "YS05";
            //再次处理维度信息
            ctrlAggVo = ctrlvoGenerator.processDimValueAgain(topBudgetCode, ctrlAggVo_OUT);

            this.budgetCtrl(topBudgetCode, "支出", actionCode, vouchers[i], ctrlAggVo, null);


        }

        return resultList.toArray(new OperationResultVO[0]);
    }

    /**
     * 预算控制
     * @param topBudgetCode
     * @param otherFilter
     * @param actionCode
     * @param ctrlAggVo
     * @param oldCtrlAggVo
     * @throws BusinessException
     */
    private void budgetCtrl(String topBudgetCode,String otherFilter,String actionCode,VoucherVO voucher,AggBmCtrlVoucherVO ctrlAggVo, AggBmCtrlVoucherVO oldCtrlAggVo) throws BusinessException{

        if(ctrlAggVo != null || oldCtrlAggVo != null){
            NCLocator.getInstance().lookup(IBmBudgetControlService.class).budgetControl(ctrlAggVo.getParentVO().getPk_group(),
                    ctrlAggVo.getParentVO().getPk_org(), "C0", null, otherFilter, actionCode, ctrlAggVo, oldCtrlAggVo, new String[]{topBudgetCode});

            OperationResultVO result = BmCtrlVoucherVOGenerator.getBmCtrlResult(ctrlAggVo, voucher);

            if(result != null)
                resultList.add(result);

        }

    }

    @Override
    public OperationResultVO[] beforeAbandon(VoucherOperateInterfaceVO arg0)
            throws BusinessException {
        return null;
    }
}
